package easier.baidu.tts;

import android.content.Context;
import android.text.TextUtils;
import com.baidu.tts.chainofresponsibility.logger.LoggerProxy;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;

public class BaiduTTSManager {

    private String appId;
    private String appKey;
    private String secretKey;

    private SpeechSynthesizer mSpeechSynthesizer;
    // TtsMode.MIX; 离在线融合，在线优先； TtsMode.ONLINE 纯在线； 没有纯离线
    private final TtsMode ttsMode = TtsMode.ONLINE;

    public BaiduTTSManager( Context context, String appId, String appKey, String secretKey ) {

        this.appId = appId;
        this.appKey = appKey;
        this.secretKey = secretKey;

        // 日志更新在UI中，可以换成MessageListener，在logcat中查看日志
        SpeechSynthesizerListener listener = provideSpeechSynthesizerListener();

        // 1. 获取实例
        mSpeechSynthesizer = SpeechSynthesizer.getInstance();
        mSpeechSynthesizer.setContext( context );

        // 2. 设置listener
        mSpeechSynthesizer.setSpeechSynthesizerListener( listener );

        // 3. 设置appId，appKey.secretKey
        int result = mSpeechSynthesizer.setAppId( appId );
        result = mSpeechSynthesizer.setApiKey( appKey, secretKey );

        // 5. 以下setParam 参数选填。不填写则默认值生效
        // 设置在线发声音人： 0 普通女声（默认） 1 普通男声  3 情感男声<度逍遥> 4 情感儿童声<度丫丫>
        mSpeechSynthesizer.setParam( SpeechSynthesizer.PARAM_SPEAKER, "0" );
        // 设置合成的音量，0-15 ，默认 5
        mSpeechSynthesizer.setParam( SpeechSynthesizer.PARAM_VOLUME, "9" );
        // 设置合成的语速，0-15 ，默认 5
        mSpeechSynthesizer.setParam( SpeechSynthesizer.PARAM_SPEED, "5" );
        // 设置合成的语调，0-15 ，默认 5
        mSpeechSynthesizer.setParam( SpeechSynthesizer.PARAM_PITCH, "5" );

        // 6. 初始化
        result = mSpeechSynthesizer.initTts( ttsMode );

        LoggerProxy.printable( printable() );
    }

    public void speak( CharSequence text ) {
        /* 以下参数每次合成时都可以修改
         *  mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0");
         *  设置在线发声音人： 0 普通女声（默认） 1 普通男声  3 情感男声<度逍遥> 4 情感儿童声<度丫丫>
         *  mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "5"); 设置合成的音量，0-9 ，默认 5
         *  mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, "5"); 设置合成的语速，0-9 ，默认 5
         *  mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, "5"); 设置合成的语调，0-9 ，默认 5
         *
         */

        if ( mSpeechSynthesizer == null ) {
            return;
        }
        if ( TextUtils.isEmpty( text ) ) {
            return;
        }

        int result = mSpeechSynthesizer.speak( text.toString() );
    }

    public void stop() {

        int result = mSpeechSynthesizer.stop();
    }

    protected SpeechSynthesizerListener provideSpeechSynthesizerListener() {

        return new SpeechSynthesizerListener() {
            @Override
            public void onSynthesizeStart( String s ) {

            }

            @Override
            public void onSynthesizeDataArrived( String s, byte[] bytes, int i, int i1 ) {

            }

            @Override
            public void onSynthesizeFinish( String s ) {

            }

            @Override
            public void onSpeechStart( String s ) {

            }

            @Override
            public void onSpeechProgressChanged( String s, int i ) {

            }

            @Override
            public void onSpeechFinish( String s ) {

            }

            @Override
            public void onError( String s, SpeechError speechError ) {

            }
        };
    }

    protected boolean printable() {

        return true;
    }
}
